/**
 * $Id: SMGP13DeliverMessage.java,v 1.3 2006/09/15 01:59:34 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.smgpmsg;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SMGP13DeliverMessage extends SMGPDeliverMessage {
    public SMGP13DeliverMessage(byte buf[]) throws IllegalArgumentException {
        super(buf);
    }

    // 以下是状态报告的处理
    // 状态报告对应原消息的msgid
    public byte[] getStatusMsgId() {
        if (1 == deliverType) {
            byte tmpId[] = new byte[10];
            System.arraycopy(buf, 73, tmpId, 0, 10);
            return tmpId;
        } else {
            return null;
        }
    }

    public String getStatusMsgIdStr() {
        if (1 == deliverType) {
            return SMGPSequenceSower.getTeleComMsgId(getStatusMsgId());
        } else {
            return null;
        }
    }

    public Date getSubmitTime() {
        if (deliverType == 1) {
            byte tmpbyte[] = new byte[4];
            System.arraycopy(buf, 83, tmpbyte, 0, 4);
            String tmpstr = new String(tmpbyte);
            int tmpYear = Integer.parseInt(tmpstr);
            tmpbyte = new byte[2];
            System.arraycopy(buf, 87, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMonth = Integer.parseInt(tmpstr) - 1;
            System.arraycopy(buf, 89, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpDay = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 91, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpHour = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 93, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMinute = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 95, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpSec = Integer.parseInt(tmpstr);
            Calendar calendar = Calendar.getInstance();
            calendar.set(tmpYear, tmpMonth, tmpDay, tmpHour, tmpMinute, tmpSec);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public Date getDoneTime() {
        if (deliverType == 1) {
            byte tmpbyte[] = new byte[4];
            System.arraycopy(buf, 97, tmpbyte, 0, 4);
            String tmpstr = new String(tmpbyte);
            int tmpYear = Integer.parseInt(tmpstr);
            tmpbyte = new byte[2];
            System.arraycopy(buf, 101, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMonth = Integer.parseInt(tmpstr) - 1;
            System.arraycopy(buf, 103, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpDay = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 105, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpHour = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 107, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMinute = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 109, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpSec = Integer.parseInt(tmpstr);
            Calendar calendar = Calendar.getInstance();
            calendar.set(tmpYear, tmpMonth, tmpDay, tmpHour, tmpMinute, tmpSec);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public String getStat() {
        if (deliverType == 1) {
            byte tmpStat[] = new byte[7];
            System.arraycopy(buf, 111, tmpStat, 0, 7);
            return (new String(tmpStat)).trim();
        } else {
            return null;
        }
    }

    public String getErr() {
        if (deliverType == 1) {
            byte tmpErr[] = new byte[3];
            System.arraycopy(buf, 118, tmpErr, 0, 3);
            return (new String(tmpErr)).trim();
        } else {
            return null;
        }
    }

    public String toString() {
        StringBuffer strBuf = new StringBuffer("SMGP13DeliverMessage: ");
        strBuf.append("Sequence_Id=").append(getSequenceId());
        strBuf.append(",MsgID=").append(getMsgIdStr());
        strBuf.append(",IsReport=").append(getIsReport());
        strBuf.append(",MsgFormat=").append(getMsgFormat());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        if (getRecvTime() != null)
            strBuf.append(",RecvTime=")
                    .append(dateFormat.format(getRecvTime()));
        else
            strBuf.append(",RecvTime=null");
        strBuf.append(",SrcTermID=").append(getSrcTermID());
        strBuf.append(",DestTermID=").append(getDestTermID());
        strBuf.append(",MsgLength=").append(getMsgLength());
        if (0 == deliverType) {
            strBuf.append(",MsgContent=").append(new String(getMsgContent()));
        } else if (1 == deliverType) {
            strBuf.append(",Submitmsgid=").append(getStatusMsgIdStr());
            strBuf.append(",SubmitTime=").append(dateFormat.format(getSubmitTime()));
            strBuf.append(",DoneTime=").append(dateFormat.format(getDoneTime()));
            strBuf.append(",Stat=").append(getStat());
            strBuf.append(",Err=").append(getErr());
        }
        strBuf.append(",Reserve=").append(getReserve());
        return strBuf.toString();
    }

}
